/* ========================================================================
 公共提交函数
 ========================================================================*/
(function($){

    'use strict';
    function ApplySubmit(element,api){
        this.element=element;
        this.options=this.element.data();
        this.form=$(this.options.form);
        this.postApi= api || 'http://pms.rebonhome.com/apply_save.html';
        this.tokenApi='http://pms.rebonhome.com/apply_token.html';
        this.mobileApi='http://pms.rebonhome.com/apply_edit.html';
        this.locationApi='http://pms.rebonhome.com/apply_phoneaddr.html';
        this.ispass=true;

        if(!this.form.length){
            this.form =this.element.closest("form");
        }

    }

    ApplySubmit.prototype.handle=function(){//主方法
        this.data = this.form.serializeArray();
        /*提交前回调*/
        this.ispass=this.callBack(this.options.before,this);
        if(this.ispass || typeof this.ispass =='undefined'){
            /*提交*/
            this.postData()
        }
    };
    ApplySubmit.prototype.callBack=function(type,parm,json){//前后回调

        if(typeof type !== 'undefined'){

            var callfun = type
            if (typeof callfun === 'string')
                callfun = callfun.toFunc();
            if (typeof callfun === 'function')
                callfun = callfun.apply(this,[parm,json])
        }

        return callfun;
    };
    ApplySubmit.prototype.comSuccess=function(applyMobile){//公共回调
        var template= '<div class="apply_success"><h3>恭喜您抢到免费量尺设计服务！</h3><p>您用<em>'+applyMobile+'</em>的号码抢到免费量尺设计服务！</p>'+
            '<p>您已获得<em>2000元</em>家具设计大礼包<em>（内含1800元设计套餐+200元代金券）</em>，我们的客服人员会在<em>24小时</em>内与您联系沟通，请您留意接听电话，祝您生活愉快!</p>'+
            '</div>';
        this.form.addClass('comsuccess').append(template);
    };
    ApplySubmit.prototype.runScAfter=function(){//提交成功转化
        $("body").append('<iframe style="display:none" src="http://pms.rebonhome.com/site_scafter.html?kid='+ACT_KID+'"><iframe/>');
    };

    ApplySubmit.prototype.postData=function(){//提交数据
        var _this=this;
        $.ajaxUrl('Post',_this.tokenApi,{},'jsonp',false,function(res){
            if(res.code==200){
                _this.data.push(
                    {name:'token',value:res.result},
                    {name:'kid',value:ACT_KID}
                );

                $.ajaxUrl('Post',_this.postApi,_this.data,'jsonp',false,function(pres){
                    if(pres.code==200){
                        /*成功*/
                        _this.runScAfter();
                        if( typeof _this.options.comsuccess!=='undefined'){/*公用回调*/

                            _this.comSuccess(pres.result.mobile);
                        }
                        if(typeof _this.options.mobile!== 'undefined'){/*手机号码归属地*/
                        	/*手机号码归属地*/
                            $.ajaxUrl('GET',_this.locationApi,{phone_token:res.extend.phone_token,phone:res.result.mobile},'json',true,function(data){
                                   var $locationData={
                                         province:data.result.result.showapi_res_body.prov,
                                         city:data.result.result.showapi_res_body.city,
                                         id: res.result.id,
                                   };
                               $.ajaxUrl('Post',mobileApi,$locationData,'json',true);
                           })
                        }
                    }
                    _this.callBack(_this.options.after,_this,pres);
                });
            }

        });


    };
    /*===插件定义===*/
    function Plugin() {
        return this.each(function () {
            var $this=$(this);
            var data=$this.data('apply');
            var api=$this.data('api');
            if(!data){
                $this.data('apply', (data=new ApplySubmit($this, api)))
            }
            data.handle();

        })
    }

    /*调用*/
    $.fn.ApplySubmit= Plugin;

})(window.jQuery || window.Zepto);

/* ========================================================================
 未提交报名函数
 ========================================================================*/

function uncommit(mobileinp){
    var uncommitApi= 'http://pms.rebonhome.com/uncommit_save.html';
    var mobileApi='http://pms.rebonhome.com/uncommit_edit.html';
    var locationApi='http://pms.rebonhome.com/apply_phoneaddr.html';

    mobileinp.blur(function(){
        var $form =$(this).closest("form");
        if($(this).val().isMobile()){//手机号码完整

            var $data = $form.serializeArray();
            $data.push({name:'kid',value:ACT_KID})

            $.ajaxUrl('Post',uncommitApi,$data,'jsonp',true,function(res){
                if(res.code==200){
                    /*手机号码归属地*/
                	/*手机号码归属地*/
                    $.ajaxUrl('GET',_this.locationApi,{phone_token:res.extend.phone_token,phone:res.result.mobile},'json',true,function(data){
                           var $locationData={
                                 province:data.result.result.showapi_res_body.prov,
                                 city:data.result.result.showapi_res_body.city,
                                 id: res.result.id,
                           };
                       $.ajaxUrl('Post',mobileApi,$locationData,'json',true);
                   })
                }
            });

        }
    })
}

/*报名申请调用*/
(function(){

    var $applybtn=$(document).find("[data-toggle='applysubmit']");
    var $mobileinp=$(document).find("[data-toggle='uncommit']");

    /*报名提交*/
    $applybtn.Validate({
        success:function(obj){
            $(obj).ApplySubmit();
        }
    });
    /*未报名提交*/
    uncommit($mobileinp);


})(window.jQuery || window.Zepto);


